﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Pingy
{
    public class DelegateCommand<T> : ICommand
    {
        private Action<T> _executeAction;
        private Predicate<T> _canExecuteAction;

        public DelegateCommand(Action<T> executeAction)
        {
            if (executeAction == null)
            {
                throw new ArgumentNullException("Execute action must have a value");
            }

            _executeAction = executeAction;
        }

        public DelegateCommand(Action<T> executeAction, Predicate<T> canExecuteAction)
            : this(executeAction)
        {
            _canExecuteAction = canExecuteAction;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            if (_canExecuteAction != null)
            {
                return _canExecuteAction((T)parameter);
            }
            else
            {
                return true;
            }
        }


        public void Execute(object parameter)
        {
            _executeAction((T)parameter);
        }
    }
}
